#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class SubsidyRecordForStudentProvider : CY.GFive.Core.Providers.DALProvider.ISubsidyRecordForStudentProvider
	{
		#region Sql
		private static readonly string SqlSelectSubsidyRecordForStudent = "SELECT [Id], [SubsidyID],[Receiver],[AcidLevel],[Amount],[Description],[CheckState],[Date] FROM [SubsidyRecordForStudent] WHERE  [Id]=@Id";

		private static readonly string SqlInsertSubsidyRecordForStudent = "INSERT INTO [SubsidyRecordForStudent]([SubsidyID],[Receiver],[AcidLevel],[Amount],[Description],[CheckState],[Date]) values(@SubsidyID,@Receiver,@AcidLevel,@Amount,@Description,@CheckState,@Date);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateSubsidyRecordForStudent = "UPDATE [SubsidyRecordForStudent] SET [SubsidyID] = @SubsidyID,[Receiver] = @Receiver,[AcidLevel] = @AcidLevel,[Amount] = @Amount,[Description] = @Description,[CheckState] = @CheckState,[Date] = @Date WHERE  [Id]=@Id";

		private static readonly string SqlDeleteSubsidyRecordForStudent = "DELETE [SubsidyRecordForStudent] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllSubsidyRecordForStudent = "SELECT [Id], [SubsidyID],[Receiver],[AcidLevel],[Amount],[Description],[CheckState],[Date] FROM [SubsidyRecordForStudent]";
		#endregion

		#region ISubsidyRecordForStudentProvider Members
		public Core.Business.SubsidyRecordForStudent Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectSubsidyRecordForStudent);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.SubsidyRecordForStudent subsidyRecordForStudent = new Core.Business.SubsidyRecordForStudent();

				if (!reader.IsDBNull(0)) subsidyRecordForStudent.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) subsidyRecordForStudent.SubsidyID = reader.GetInt32(1);
				if (!reader.IsDBNull(2)) subsidyRecordForStudent.Receiver = reader.GetInt32(2);
				if (!reader.IsDBNull(3)) subsidyRecordForStudent.AcidLevel = reader.GetByte(3);
				if (!reader.IsDBNull(4)) subsidyRecordForStudent.Amount = reader.GetDecimal(4);
				if (!reader.IsDBNull(5)) subsidyRecordForStudent.Description = reader.GetString(5);
				if (!reader.IsDBNull(6)) subsidyRecordForStudent.CheckState = reader.GetByte(6);
				if (!reader.IsDBNull(7)) subsidyRecordForStudent.Date = reader.GetDateTime(7);

				return subsidyRecordForStudent;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.SubsidyRecordForStudent subsidyRecordForStudent)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@SubsidyID", SqlDbType.Int, subsidyRecordForStudent.SubsidyID);
			sql.AddParameter("@Receiver", SqlDbType.Int, subsidyRecordForStudent.Receiver);
			sql.AddParameter("@AcidLevel", SqlDbType.TinyInt, subsidyRecordForStudent.AcidLevel);
			sql.AddParameter("@Amount", SqlDbType.Decimal, subsidyRecordForStudent.Amount);
			sql.AddParameter("@Description", SqlDbType.NVarChar, subsidyRecordForStudent.Description);
			sql.AddParameter("@CheckState", SqlDbType.TinyInt, subsidyRecordForStudent.CheckState);
			sql.AddParameter("@Date", SqlDbType.DateTime, subsidyRecordForStudent.Date);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertSubsidyRecordForStudent);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) subsidyRecordForStudent.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.SubsidyRecordForStudent subsidyRecordForStudent)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@SubsidyID", SqlDbType.Int, subsidyRecordForStudent.SubsidyID);
			sql.AddParameter("@Receiver", SqlDbType.Int, subsidyRecordForStudent.Receiver);
			sql.AddParameter("@AcidLevel", SqlDbType.TinyInt, subsidyRecordForStudent.AcidLevel);
			sql.AddParameter("@Amount", SqlDbType.Decimal, subsidyRecordForStudent.Amount);
			sql.AddParameter("@Description", SqlDbType.NVarChar, subsidyRecordForStudent.Description);
			sql.AddParameter("@CheckState", SqlDbType.TinyInt, subsidyRecordForStudent.CheckState);
			sql.AddParameter("@Date", SqlDbType.DateTime, subsidyRecordForStudent.Date);
			sql.AddParameter("@Id", SqlDbType.Int, subsidyRecordForStudent.Id);
			sql.ExecuteSql(SqlUpdateSubsidyRecordForStudent);
		}

		public void Delete(Core.Business.SubsidyRecordForStudent subsidyRecordForStudent)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, subsidyRecordForStudent.Id);
			sql.ExecuteSql(SqlDeleteSubsidyRecordForStudent);
		}

		public IList<Core.Business.SubsidyRecordForStudent> GetAllSubsidyRecordForStudent()
		{
			IList<Core.Business.SubsidyRecordForStudent> subsidyRecordForStudentlist = new List<Core.Business.SubsidyRecordForStudent>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllSubsidyRecordForStudent);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.SubsidyRecordForStudent subsidyRecordForStudent = new Core.Business.SubsidyRecordForStudent();

					if (!reader.IsDBNull(0)) subsidyRecordForStudent.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) subsidyRecordForStudent.SubsidyID = reader.GetInt32(1);
					if (!reader.IsDBNull(2)) subsidyRecordForStudent.Receiver = reader.GetInt32(2);
					if (!reader.IsDBNull(3)) subsidyRecordForStudent.AcidLevel = reader.GetByte(3);
					if (!reader.IsDBNull(4)) subsidyRecordForStudent.Amount = reader.GetDecimal(4);
					if (!reader.IsDBNull(5)) subsidyRecordForStudent.Description = reader.GetString(5);
					if (!reader.IsDBNull(6)) subsidyRecordForStudent.CheckState = reader.GetByte(6);
					if (!reader.IsDBNull(7)) subsidyRecordForStudent.Date = reader.GetDateTime(7);

					subsidyRecordForStudent.MarkOld();
					subsidyRecordForStudentlist.Add(subsidyRecordForStudent);
				}
				reader.Close();
			}
			return subsidyRecordForStudentlist;
		}
		#endregion
	}
}
